﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Text;
using System.Web;
using System.Xml;
using System.Xml.Serialization;

namespace Alogient.Cameleon.Sdk.UrlRewriting
{
    public class RewritingConfigHandler : IConfigurationSectionHandler
    {
        public object Create(object parent, object configContext, System.Xml.XmlNode section)
        {
            // Create an instance of XmlSerializer based on the RewriterConfiguration type...
            XmlSerializer ser = new XmlSerializer(typeof(RewritingConfig));

            // Return the Deserialized object from the Web.config XML
            return ser.Deserialize(new XmlNodeReader(section));
        }
    }

    [Serializable()]
    [XmlRoot("RewritingConfig")]
    public class RewritingConfig
    {
        // private member variables
        private RewriterRuleCollection ruleSet;			// an instance of the RewriterRuleCollection class...

        /// <summary>
        /// GetConfig() returns an instance of the <b>RewriterConfiguration</b> class with the values populated from
        /// the Web.config file.  It uses XML deserialization to convert the XML structure in Web.config into
        /// a <b>RewriterConfiguration</b> instance.
        /// </summary>
        /// <returns>A <see cref="RewriterConfiguration"/> instance.</returns>
        public static RewritingConfig GetConfig()
        {
            if (HttpContext.Current.Cache["RewritingConfig"] == null)
            {
                HttpContext.Current.Cache.Insert("RewritingConfig", ConfigurationManager.GetSection("RewritingConfig"));
            }

            return (RewritingConfig)HttpContext.Current.Cache["RewritingConfig"];
        }

        #region Public Properties
        /// <summary>
        /// A <see cref="RewriterRuleCollection"/> instance that provides access to a set of <see cref="RewriterRule"/>s.
        /// </summary>
        public RewriterRuleCollection RuleSet
        {
            get
            {
                return ruleSet;
            }
            set
            {
                ruleSet = value;
            }
        }
        #endregion
    }

    [Serializable()]
    public class RewriterRuleCollection : CollectionBase
    {
        /// <summary>
        /// Adds a new RewriterRule to the collection.
        /// </summary>
        /// <param name="r">A RewriterRule instance.</param>
        public virtual void Add(Rule r)
        {
            this.InnerList.Add(r);
        }

        /// <summary>
        /// Gets or sets a RewriterRule at a specified ordinal index.
        /// </summary>
        public Rule this[int index]
        {
            get
            {
                return (Rule)this.InnerList[index];
            }
            set
            {
                this.InnerList[index] = value;
            }
        }
    }

    [Serializable()]
    public class Rule
    {
        // private member variables...
        private string source, destination;

        #region Public Properties
        /// <summary>
        /// Gets or sets the pattern to look for.
        /// </summary>
        public string Source
        {
            get
            {
                return source;
            }
            set
            {
                source = value;
            }
        }

        /// <summary>
        /// The string to replace the pattern with, if found.
        /// </summary>
        public string Destination
        {
            get
            {
                return destination;
            }
            set
            {
                destination = value;
            }
        }
        #endregion
    }
}
